package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.sql.Time;
import java.util.PropertyResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
public class Main6Activity extends AppCompatActivity {
    private  Button bt;
    private Timer timer;
    private TimerTask task;
    private Handler handler;
    private ImageView image1,image2,image3,image4,image5;
    private final int count = 5;
    private int imgIndex;
    final int[] names = new int[] {
            R.id.image1,
            R.id.image2,
            R.id.image3,
            R.id.image4,
            R.id.image5
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);
        image3 = (ImageView) findViewById(R.id.image3);
        image4 = (ImageView) findViewById(R.id.image4);
        image5 = (ImageView) findViewById(R.id.image5);

        for(int i = 0;i < 5;i++){
            names[i] = getResources().getIdentifier("image" + (i+1),"drawable", "com.example.myapplication");
        }
        handler = new Handler () {
            @Override
            public void handleMessage(Message msg) {

                image1.setImageResource(names[msg.what]);
                image2.setImageResource(names[msg.what]);
                image3.setImageResource(names[msg.what]);
                image4.setImageResource(names[msg.what]);
                image5.setImageResource(names[msg.what]);
            }
        };

        bt = (Button) findViewById(R.id.button9);//id后面为上方button的id

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(Main6Activity.this, MainActivity.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });

    }
    /**
     * 开始按钮单击事件处理方法
     *
     * @param view
     */
    public void doStart(View view) {
        // 实例化定时器
        timer = new Timer();

        // 实例化定时器任务
        task = new TimerTask() {
            @Override
            public void run() {
                // 更新图像索引
                imgIndex++;
                imgIndex = imgIndex % count;
                // 将更新后的图像索引作为消息内容发送给主线程
                handler.sendEmptyMessage(imgIndex);
            }
        };

        timer.schedule(task, 0, 1000);
    }

    /**
     * 停止按钮单击事件处理方法
     *
     * @param view
     */
    public void doStop(View view) {
        // 取消定时器任务
        timer.cancel();
    }
}
